libname mylib 'd:\Dropbox\Wall Street Bets (Private)\Data';


*This program contains the information needed to create Table 5 of the paper;
*The program references daily_panel_rfs which is constructed in  Daily Panel RFS (Intermediate File) code;


*The code below reports specification 1 of Table 3 (i.e., 5-day ahead returns and the full sample of stocks).
* To obtain Specification 2, replace Ret5 with Ret 21.
* To obtain Specification 3 impose the filter "where gme_amc_flag  =0" by removing the "*" in the first line of code;
* To obtain Specification 4 replace Ret5 with Ret 21 and impose the filter "where gme_amc_flag  =0" by removing the "*" in the first part of code;



*note the code below reports results from two seperate regressions;
*the first regressions provides estiamtes for the pre-pandemic and post-pandemic. The estimate from this regression on net_dd_post captures the average effect, not the incremental effect 
that is typically reported throughout the paper. The 2nd regression reports the incremental effect for net dd post ;




DATA INFO;
SET MYLIB.DAILY_PANEL_RFS;


IF CUM_MONTH <=25 THEN PRE_PANDEMIC =1;

IF CUM_MONTH = 26 AND DAY <=25 THEN PRE_PANDEMIC =1;
IF CUM_MONTH = 26 AND DAY >=26 THEN PRE_PANDEMIC =0;


IF CUM_MONTH >26 THEN PRE_PANDEMIC =0;
POST_PANDEMIC = 1 - PRE_PANDEMIC;


if post_gme =1 then pre_pandemic =0;
if post_gme = 1 then post_pandemic =0;



net_dd2_PRE_PANDEMIC = net_dd2 * PRE_PANDEMIC;
net_dd2_POST_PANDEMIC = net_dd2 * POST_PANDEMIC;


NON_RESEARCH2_pre_pandemic = NON_RESEARCH2 * pre_pandemic;
NON_RESEARCH2_post_pandemic = NON_RESEARCH2 * post_pandemic;




net_sa2_pre_pandemic = net_sa2 * pre_pandemic;
net_sa2_post_pandemic = net_sa2 * post_pandemic;





keep keep ticker date cum_month gme_amc_flag post_gme
ret5 ret21
net_dd2  NET_DD2_POST NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST
DD_and_SA DD_and_SA_POST DD_and_Non_Research DD_and_Non_Research_post net_dd2_pre
 ln_size ln_bm  abn_ret mom5 mom6_26     
BM_MISSING SIZE_MISSING   news_sentiment lag_sent5 lag_sent6_26 SENTIMENT21
total_dd_posts net_dd2_PRE_PANDEMIC net_dd2_POST_PANDEMIC NON_RESEARCH2_pre_pandemic NON_RESEARCH2_post_pandemic
net_sa2_pre_pandemic net_sa2_post_pandemic  pre_pandemic post_pandemic;
*WHERE GME_AMC_FLAG =0;
RUN;


proc univariate data=info;
var net_dd2_POST_PANDEMIC;
where net_dd2_POST_PANDEMIC >0;
run;



PROC SORT DATA=info;
BY DATE;
RUN;
PROC STANDARD DATA=info OUT=INFO_FE MEAN=0;
BY DATE;
var ret5 ret21
net_dd2  NET_DD2_POST NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST
DD_and_SA DD_and_SA_POST DD_and_Non_Research DD_and_Non_Research_post net_dd2_pre
 ln_size ln_bm  abn_ret mom5 mom6_26     
BM_MISSING SIZE_MISSING   news_sentiment lag_sent5 lag_sent6_26 SENTIMENT21 
net_dd2_PRE_PANDEMIC net_dd2_POST_PANDEMIC NON_RESEARCH2_pre_pandemic NON_RESEARCH2_post_pandemic
net_sa2_pre_pandemic net_sa2_post_pandemic  pre_pandemic post_pandemic;

RUN;



*provides estimates on pre-pandemic and post-pandemic;

%let yourdata=INFO_FE;

%let firmid=ticker ;
%let time=cum_month;

%let y=RET5 ;
%let x=    net_dd2_PRE_PANDEMIC net_dd2_POST_PANDEMIC  NET_DD2_POST   NON_RESEARCH2_pre_pandemic NON_RESEARCH2_post_pandemic  NON_RESEARCH2_POST
 net_sa2_pre_pandemic net_sa2_post_pandemic  NET_SA2_POST ln_size ln_bm  abn_ret mom5 mom6_26       news_sentiment lag_sent5 lag_sent6_26   BM_MISSING SIZE_MISSING;
RUN;




proc surveyreg data=&yourdata;

    cluster &firmid;
    model &y = &x /covb;
    ods output covb=firm;
run;
quit;


*cluster by second dimension (e.g., year);
proc surveyreg data=&yourdata;

    cluster &time;
    model &y = &x /covb;
    ods output covb=year;
run;
quit;

*cluster by intersection of the two dimensions (e.g, firm-year);
proc surveyreg data=&yourdata;
    cluster &firmid &time;
    model &y = &x /covb;
    ods output covb=both;
    ods output parameterestimates=parm;
run;
quit;

*keeps original parameter estimates;
data parm; set parm;
    keep parameter estimate;
run;

*returns a dataset with a scalar for the dimensions of the var/cov matrix. This is needed to extract the square roots of the diagonals later on;
data parm1; set parm;
    n=_n_;
    m=1;
    keep m n;
run;

data parm1; set parm1; by m;
    if last.m;
    keep n;
run;

*uses matrix algebra interface to construct Var-cov matrix and extract the standard errors;
proc iml;
    use both;
    read all var _num_ into Z;
    print Z;
    use firm;
    read all var _num_ into X;
    print X;
    use year;
    read all var _num_ into Y;
    print Y;
    use parm1;
    read all var _num_ into n;
    print n;

    B=X+Y-Z;
    C=I(n);
    D=J(n,1);
    E=C#B;
    F=E*D;
    G=F##.5;

    print B;
    print G;
    create b from G [colname='stderr']; ;
    append from G;
quit;

*creates a dataset called 'results' that contains the parameter estimates, the SE's, and the t-stats;
data results; merge parm B;
    tstat=estimate/stderr;
run;

proc print data=results;
run;




data spec3;
set results;
estimate3 = estimate  ;
obs = _n_;
keep parameter estimate3 obs;
run;
data spec3b;
set results;
estimate3 = tstat ;
obs = _n_ + .5;
keep parameter estimate3 obs;
run;
data spec3;
set spec3 spec3b;
run;
proc sort data=spec3;
by obs;
run;



*provides estimates on post;

%let yourdata=INFO_FE;

%let firmid=ticker ;
%let time=cum_month;

%let y=RET5 ;
%let x=    net_dd2 net_dd2_POST_PANDEMIC  NET_DD2_POST   NON_RESEARCH2_pre_pandemic NON_RESEARCH2_post_pandemic  NON_RESEARCH2_POST
 net_sa2_pre_pandemic net_sa2_post_pandemic  NET_SA2_POST ln_size ln_bm  abn_ret mom5 mom6_26       news_sentiment lag_sent5 lag_sent6_26   BM_MISSING SIZE_MISSING;
RUN;




proc surveyreg data=&yourdata;

    cluster &firmid;
    model &y = &x /covb;
    ods output covb=firm;
run;
quit;


*cluster by second dimension (e.g., year);
proc surveyreg data=&yourdata;

    cluster &time;
    model &y = &x /covb;
    ods output covb=year;
run;
quit;

*cluster by intersection of the two dimensions (e.g, firm-year);
proc surveyreg data=&yourdata;
    cluster &firmid &time;
    model &y = &x /covb;
    ods output covb=both;
    ods output parameterestimates=parm;
run;
quit;

*keeps original parameter estimates;
data parm; set parm;
    keep parameter estimate;
run;

*returns a dataset with a scalar for the dimensions of the var/cov matrix. This is needed to extract the square roots of the diagonals later on;
data parm1; set parm;
    n=_n_;
    m=1;
    keep m n;
run;

data parm1; set parm1; by m;
    if last.m;
    keep n;
run;

*uses matrix algebra interface to construct Var-cov matrix and extract the standard errors;
proc iml;
    use both;
    read all var _num_ into Z;
    print Z;
    use firm;
    read all var _num_ into X;
    print X;
    use year;
    read all var _num_ into Y;
    print Y;
    use parm1;
    read all var _num_ into n;
    print n;

    B=X+Y-Z;
    C=I(n);
    D=J(n,1);
    E=C#B;
    F=E*D;
    G=F##.5;

    print B;
    print G;
    create b from G [colname='stderr']; ;
    append from G;
quit;

*creates a dataset called 'results' that contains the parameter estimates, the SE's, and the t-stats;
data results; merge parm B;
    tstat=estimate/stderr;
run;

proc print data=results;
run;




data spec3;
set results;
estimate3 = estimate  ;
obs = _n_;
keep parameter estimate3 obs;
run;
data spec3b;
set results;
estimate3 = tstat ;
obs = _n_ + .5;
keep parameter estimate3 obs;
run;
data spec3;
set spec3 spec3b;
run;
proc sort data=spec3;
by obs;
run;
